<?php
class accountsModel {
	var $DB;
	function accountsModel ($DB){
		$this->DB = $DB;
	}

	function login ($email,$pwd){
		session_start();
		$sql = "SELECT uid,username,password,email from member WHERE email = '$email' ";
		$this->DB->query($sql);
		$record = $this->DB->fetchRow();
		if (!$record){
			return false;
		}else{
			$check_pwd = md5($pwd);
			if($record['password'] == $check_pwd){
				global $onlineip;
				$timestamp = time();
				$this->DB->query("UPDATE member SET lastip='$onlineip',lasttime='$timestamp',loginnum = loginnum+1 WHERE email = '$email'");
				//todo
				$_SESSION['username']	= $record['username'];
				$_SESSION['email']	=  $email;
				$_SESSION['uid'] = $record['uid'];

				return true;
			}else{
				return false;
			}
		}
	}

	function regUser($username,$email,$password){
		$sql = "SELECT username from member WHERE username = '$username' ";
		$this->DB->query($sql);
		$record = $this->DB->fetchRow();
		if ($record){
			return 'no';
		}else{
			global $timestamp,$onlineip;
			$password = md5($password);
			$sql = "INSERT INTO member (username,password,email,regip,regtime,loginnum) VALUES ('$username','$password','$email','$onlineip','$timestamp','1')";
			$this->DB->query($sql);
			return 'yes';
		}
	}

	function isLoginned() {
		if(!empty($_SESSION['username']))
			return true;
		else
			return false;
	}

	function logout(){
		$_SESSION = array();
		 unset($_SESSION);
	}

	function getUserbasic($username){
		$sql = "SELECT * FROM member WHERE username = '$username'";
		$this->DB->query($sql);
		return $this->DB->fetchRow();
	}

	function  editPassword($uid,$pwd){
		$newpw = md5($pwd);
		$sql = "UPDATE member SET password ='$newpw' WHERE uid='$uid' ";
		$this->DB->query($sql);	
		return $this->DB->affectedRows();
	}

	function editUser($uid,$password,$tel,$email){
		$sql = "UPDATE member SET password ='$password',email='$email',tel='$tel' WHERE uid = '$uid' ";
		//de($sql);
		$this->DB->query($sql);	
		return $this->DB->affectedRows();
	}
}
?>
